﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using HRM.DAL.DAO;

namespace HRM.DAL.Gateway
{
    public class PromotionGateway:DBGateway
    {
        public bool SavePromotion(int empId,Promotion promotion)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format(@"insert into tblPromotion values(@empid,@predesig,@presentdesig,@date)");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@empid", SqlDbType.Int).Value = empId;
                CommandObj.Parameters.Add("@predesig", SqlDbType.VarChar).Value = promotion.PreDesignation;
                CommandObj.Parameters.Add("@presentdesig", SqlDbType.VarChar).Value = promotion.PresentDesig;
                CommandObj.Parameters.Add("@date", SqlDbType.DateTime).Value = promotion.Date;
                CommandObj.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                throw new Exception("Error Occured During Promotion");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        public List<Promotion> GetEmpPromotionHistory(int empId)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format(@"select * from tblPromotion where EmpId='"+empId+"'");
                SqlDataReader reader = CommandObj.ExecuteReader();
                List<Promotion> promotions=new List<Promotion>();
                while (reader.Read())
                {
                    Promotion promotion=new Promotion();
                    promotion.Id = Convert.ToInt32(reader[0]);
                    promotion.PreDesignation = reader[2].ToString();
                    promotion.PresentDesig = reader[3].ToString();
                    DateTime dt=new DateTime();
                    dt = Convert.ToDateTime(reader[4]);
                    promotion.Date = dt.ToShortDateString();
                    promotions.Add(promotion);
                }
                return promotions;
            }
            catch (Exception)
            {
                throw new Exception("Error Occured during Reading Information !");
            }
            finally
            {
               ConnectionObj.Close(); 
            }
        }

        public bool UpdatePromotion(int empId, Promotion promotion)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format(@"Update tblPromotion set PreDesig=@predesig,PresentDesig=@presentdesig,Date=@date where Id='"+promotion.Id+"'");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@predesig", SqlDbType.VarChar).Value = promotion.PreDesignation;
                CommandObj.Parameters.Add("@presentdesig", SqlDbType.VarChar).Value = promotion.PresentDesig;
                CommandObj.Parameters.Add("@date", SqlDbType.DateTime).Value = promotion.Date;
                CommandObj.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                throw new Exception("Error Occured During Updataing Promotion");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }
    }
}